<?php 
use backend\models\GrpClass;
use backend\models\GrpImageType;
use yii\helpers\Url;
use backend\models\PrfType;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use backend\assets\FontawesomeAsset;

FontawesomeAsset::register($this);
$this->title =  '选择类别';
$img_tag = isset($_GET['tag'])?$_GET['tag']:'img_id';
$previous = Url::previous();
?>
	<link rel="stylesheet" type="text/css" class="library" href="tree_depends/bootstrap-2.2.1.min.css">
	<script type="text/javascript" class="library" src="tree_depends/jquery-1.7.2.min.js"></script>
	<script type="text/javascript" class="library" src="tree_depends/bootstrap-2.2.1.min.js"></script>
<script type="text/javascript">
<!--
/*
var curPage = 1;
var total,pagesize,totalPage;
var img_type_click;
var img_tag
//获取当前页数据
function getData(page,search,tag){
	img_type_click = search;
	img_tag = tag;
	$.ajax({
		//type: "POST",
		url: 'index.php?r=image/show-image',
		data: {"page":page-1,"search":search,"tag":tag},
		dataType: 'json',
		beforeSend:function(){
			$("#list ul").append("<li id='loading'>loading...</li>");
			},
		success:function(json){
			//alert(json);
			$("#list ul").empty(); //清空数据区
			total = json.total; //总记录数
			pageSize = json.pageSize;//每页显示条数
			curPage = page;//当前页
			totalPage = json.totalPage;//总页数
			var li ="";
			if( json.list != "" )
			{
				var list = json.list;
				var have;
				$.each(list,function(index,array){
					
					li += "<li><a href='<?=$previous ?>"+"&<?=$img_tag ?>=" + array['id']+ "'><img src='" + array['url'] +"' height='" + array['height'] +"' width='" + array['width'] + "'>" + array['name']+"<br/>_"+array['height']+"X"+array['width']+"</a></li>";
					have = array['url'];
					});
					if(have.length != 0){
					$("#list ul").append(li);}
			}
			else{
				$("#list ul").append("<li>暂无图片</li>");
				}
			},
		
		complete:function(){
				getPageBar();
			},
		error:function(){
				//alert("数据加载失败");
			},
		});
}

function getPageBar(){
	//页码大于最大页数，确认当前页
	if(curPage > totalPage) curPage=totalPage;
	//页码小于1，确认当前页
	if(curPage<1) curPage = 1;
	
	pageStr = "<span>共"+total+"条</span><span>"+curPage+"/"+totalPage+"</span>";
	
	//如果是第一页
	if(curPage ==1){
		pageStr += "<span>首页</span><span>上一页</span>";
	}else{
		pageStr += "<span><a href='javascript:void(0)' rel='1'>首页</a></span><span><a href='javascript:void(0)' rel='"+(curPage-1)+"'>上一页</a></span>"; 
	}
	
	//如果是最后页
	if(curPage>=totalPage){ 
    	pageStr += "<span>下一页</span><span>尾页</span></div>"; 
  	}else{ 
    	pageStr += "<span><a href='javascript:void(0)' rel='"+(parseInt(curPage)+1)+"'> 下一页</a></span><span><a href='javascript:void(0)' rel='"+totalPage+"'>尾页</a> </span>"; 
  } 
      
  $("#pagecount").html(pageStr); 

}

//打开页面，第一次加载
$(function(){ 
  getData(1,101000000,'type'); 
  $("#pagecount span a").live('click',function(){ 
    var rel = $(this).attr("rel"); 
    if(rel){ 
      getData(rel,img_type_click,img_tag); 
    } 
  }); 
}); 
*/
function show(id)
{
	$("#upload_img"+id).css("display","inline");
}
function hide(id)
{
	$("#upload_img"+id).css("display","none");
}

function upload_img(type_id)
{
	alert('ok');
}
function check_img(obj,id)
{
	if(obj.checked)
	{
		//alert(id);
		//$("#upload_img"+id).css("display","inline");
		setCookieForImageTypeid(id);

	}
	else
	{
		//$("#upload_img"+id).css("display","none");
	}
}

function setCookieForImageTypeid(typeid){

	$.ajax({
		//type: "POST",
		url: 'index.php?r=image/write-typeid-to-cookie',
		data: {"typeid":typeid},
		dataType: 'json',
		beforeSend:function(){
			//$("#list ul").append("<li id='loading'>loading...</li>");
			},
		success:function(data){
			
			//alert(data);
			},
		
		complete:function(){
			opener.location.reload();
			window.close();
			},
		error:function(){
			//	alert("数据加载失败");
			},
		});
}
/*
window.onload = function(){
	
	//alert(document.referrer);
	var referrer = $("#referrer").val();
	//alert(referrer);
	if(document.referrer == referrer)
	{
		//alert('ok');
		return false;
	}
	opener.location.reload();
};

*/
//-->
</script>

<body>


<?php $url = "index.php?r=image/upload-image&type_id="; ?>
	<div class="tree well" style='vertical-align:top;'>
	

	
	<?php 
		function tree_show($father)
		{
			$url = "index.php?r=image/upload-image&type_id=";
			$model_tree = PrfType::findAll(['father'=>$father]);
			if ($model_tree != null)
			{
				foreach ($model_tree as $value)
				{
					echo '<ul>';
					echo '<li style="display:none;">';
						$model_tree_child = PrfType::findAll(['father'=>$value->id]);
						if($model_tree_child != null)
						{
							echo '<input id="check_img('.$value->id.')" onclick="check_img(this,'.$value->id.')" type="checkbox" value="'.$value->id.'"/><span><i class="icon-plus-sign"></i><a href="#">'.$value->typename.'</a></span>';
								
							tree_show($value->id);
						}
						else if($model_tree_child == null)
						{
							echo '<input id="check_img('.$value->id.')" onclick="check_img(this,'.$value->id.')" type="checkbox" value="'.$value->id.'"/><span onclick="show_image('.$value->id.')">'.$value->typename.'</span>';
								
						}
						
					echo '</li>';
					echo '</ul>';
				}
			}		
		}
		?>
		
	<ul style='display:inline-block;border:0px solid #ccc;'>
		<li>
			<span><i class="icon-folder-open"></i>新套装</span>
					<?php 
			
			//$model_type_no_class = PrfType::findAll(['father' => '-1']);
			$model_type_no_class = PrfType::findBySql("select * from prf_type where father='-1' and addr ='newtaozhuang'")->all();
			foreach ($model_type_no_class as $key => $value)
			{
				$_addr_string = $value->addr;
				$_addr_arr = explode('/', $_addr_string);
				if(count($_addr_arr) == 1)
				{
					?>
					<ul>
						<li style="">
							
							<?php  echo '<input id="check_img('.$value->id.')" onclick="check_img(this,'.$value->id.')" type="checkbox" value="'.$value->id.'"/><span><i class="icon-plus-sign"></i>'.$value->typename.'</span>';?>
							<?php  tree_show_addr($value->addr); ?>
						</li>		
					</ul>
					<?php 
					
				}
			}
			?>
		</li>		
		</ul>
	<!-- 
		<ul style='display:inline-block;border:0px solid #ccc;'>
			<li><span><i class="icon-folder-open"></i>分类表</span>
		<?php 
		foreach (PrfType::findAll(['father'=>0]) as $value)
		{ ?>
			<ul>
				<li style="">
					<span><i class="icon-folder-open"></i><?=$value->typename ?></span>
					<?php tree_show($value->id );?>
				</li>
			</ul>
		<?php }?>		
		</li>
	</ul>　　　　　　　　　
	 -->
	<?php
	//读第几层
	function tree_show_addr($addr)
	{
		
		$url = "index.php?r=image/upload-image&type_id=";
		$model_tree_addr = PrfType::findBySql("select * from prf_type where addr REGEXP '".$addr."/[a-zA-Z0-9]*$'")->all();
		if ($model_tree_addr != null)
		{
			foreach ($model_tree_addr as $value)
			{
				echo '<ul>';
				echo '<li style="display:none;">';
				//$model_tree_child = PrfType::findAll(['father'=>$value->id]);
				//判断是否有子菜单，确定不一样的文件夹ui
				$model_tree_addr_child = PrfType::findBySql("select * from prf_type where addr REGEXP '".$value->addr."/[a-zA-Z0-9]*$'")->all();
				if($model_tree_addr_child != null)
				{
					echo '<input id="check_img('.$value->id.')" onclick="check_img(this,'.$value->id.')" type="checkbox" value="'.$value->id.'"/><span><i class="icon-plus-sign"></i><a href="#" onclick="getData(1,'.$value->id.',\'type\')"> '.$value->typename.'</a></span><br/><span style="display:none;cursor:pointer;" id="upload_img'.$value->id.'" onclick="javascript:window.open(\''.$url.$value->id.'\',\'\',\'width=800,height=600,toolbar=no, status=no, menubar=no, resizable=yes, scrollbars=yes\')">上传图片</span>';
					//$_addr_child = PrfType::findOne(['id'=>$value->id])->addr;
					tree_show_addr($value->addr);
				}
				
				else if($model_tree_addr_child == null)
				{
					echo '<input id="check_img('.$value->id.')" onclick="check_img(this,'.$value->id.')" type="checkbox" value="'.$value->id.'"/><span onclick="show_image('.$value->id.')">'.$value->typename.'</span>';
		
				}
		
				echo '</li>';
				echo '</ul>';
			}
		}
	}

	?>
	<!-- 
		<ul style='display:inline-block;border:0px solid #ccc;'>
			<li >
				<span><i class="icon-folder-open"></i>老结构</span>
				
			<?php 
			
			//$model_type_no_class = PrfType::findAll(['father' => '-1']);
			$model_type_no_class = PrfType::findBySql("select * from prf_type where father='-1' and addr not like 'newtaozhuang%'")->all();
			foreach ($model_type_no_class as $key => $value)
			{
				$_addr_string = $value->addr;
				$_addr_arr = explode('/', $_addr_string);
				if(count($_addr_arr) == 1)
				{
					?>
					<ul>
						<li style="display:none;">
							
							<?php echo '<input id="check_img('.$value->id.')" onclick="check_img(this,'.$value->id.')" type="checkbox" value="'.$value->id.'"/><span><i class="icon-plus-sign"></i>'.$value->typename.'</span>';?>
							<?php tree_show_addr($value->addr); ?>
						</li>		
					</ul>
					<?php 
					
				}
			}
			?>
			
		</li>		
	</ul>
	 -->
	<hr/>

</div>


</body>

    	<style type="text/css">.tree {
    min-height:20px;
    padding:19px;
    margin-bottom:20px;
    background-color:#fbfbfb;
    border:1px solid #999;
    -webkit-border-radius:4px;
    -moz-border-radius:4px;
    border-radius:4px;
    -webkit-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);
    -moz-box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05);
    box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.05)
}
.tree li {
    list-style-type:none;
    margin:0;
    padding:0px 5px 0px 5px;
    position:relative
}
.tree li::before, .tree li::after {
    content:'';
    left:-20px;
    position:absolute;
    right:auto
}
.tree li::before {
    border-left:1px solid #999;
    bottom:50px;
    height:100%;
    top:0;
    width:1px
}
.tree li::after {
    border-top:1px solid #999;
    height:20px;
    top:25px;
    width:25px
}
.tree li span {
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
    border:1px solid #999;
    border-radius:5px;
    display:inline-block;
    padding:1px 8px;
    text-decoration:none
}
.tree li.parent_li>span {
    cursor:pointer
}
.tree>ul>li::before, .tree>ul>li::after {
    border:0
}
.tree li:last-child::before {
    height:30px
}
.tree li.parent_li>span:hover, .tree li.parent_li>span:hover+ul li span {
    background:#eee;
    border:1px solid #94a0b4;
    color:#000
}


<!--list的-->
#list{width:680px; height:530px; margin:2px auto; position:relative} 
#list ul li{display:inline-block;width:220px; height:260px; margin:2px} 
#list ul li img{width:220px; height:220px} 
#list ul li p{line-height:22px} 
#pagecount{width:500px; margin:10px auto; text-align:center;} 
#pagecount span{margin:4px; font-size:14px} 
#list ul li#loading{width:120px; height:32px; border:1px solid #d3d3d3; 
position:absolute; top:35%; left:42%; text-align:center; background:#f7f7f7
url(loading.gif) no-repeat 8px 8px;-moz-box-shadow:1px 1px 2px rgba(0,0,0,.2); 
-webkit-box-shadow:1px 1px 2px rgba(0,0,0,.2); box-shadow:1px 1px 2px rgba(0,0,0,.2);} 


ul{list-style:none;}

</style>
    	
	    		<script>$(function () {
    $('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');
    $('.tree li.parent_li > span').on('click', function (e) {
        var children = $(this).parent('li.parent_li').find(' > ul > li');
        if (children.is(":visible")) {
            children.hide('fast');
            $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
        } else {
            children.show('fast');
            $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
        }
        e.stopPropagation();
    });
});</script>
	
<!-- Generated by RunJS (Mon Nov 30 11:35:25 CST 2015) 0ms -->